package com.hui.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.hui.base.BaseServlet;
import com.hui.pojo.Course;
import com.hui.pojo.Course_Lesson;
import com.hui.pojo.Course_Section;
import com.hui.service.CourseContentService;
import com.hui.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/07/13/10:59
 * @Description: 课程内容模块
 */
@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    private CourseContentService courseContentService = new CourseContentServiceImpl();

    // 保存和修改课时信息
    public void saveOrUpdateLesson(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 前端数据是JSON形式，要从request域中获取
            Map<String, Object> map = (Map) req.getAttribute("map");
            // 通过BeanUtils将数据封装到 lesson 中
            Course_Lesson lesson = new Course_Lesson();
            BeanUtils.populate(lesson, map);
//            lesson.setOrderNum((int) map.get("order_num"));  // 该字段名不同，需手动设置
            // 调用业务层，执行保存与修改
            String result = null;
            if (lesson.getId() == 0) {
                // 保存操作
                result = courseContentService.insertLesson(lesson);
            } else {
                // 修改操作
                result = courseContentService.updateLesson(lesson);
            }
            // 将结果写出去
            PrintWriter writer = resp.getWriter();
            writer.write(result);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 通过课程id 获取章节与课时信息
    public void findSectionAndLessonByCourseId(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 获取课程id，查询结果
            String course_id = req.getParameter("course_id");
            List<Course_Section> list = courseContentService.getSectionAndLessonByCourseId(Integer.parseInt(course_id));
            // 将结果转换成JOSN字符串格式
            String res = JSON.toJSONString(list);
            // 将结果写出去
            PrintWriter writer = resp.getWriter();
            writer.write(res);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 回显章节对应的课程信息
    public void findCourseById(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 获取参数，查询结果
            String course_id = req.getParameter("course_id");
            Course course = courseContentService.getCourseById(Integer.parseInt(course_id));
            // 将结果JSON格式化
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");
            String res = JSON.toJSONString(course, filter);
            // 将结果写出去
            PrintWriter writer = null;
            writer = resp.getWriter();
            writer.write(res);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 保存&修改章节信息
    public void saveOrUpdateSection(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 前端数据是JSON形式，要从request域中获取
            Map<String, Object> map = (Map) req.getAttribute("map");
            // 创建 Course_Section , 利用 BeanUtils 将map中的数据封装进去
            Course_Section section = new Course_Section();
            BeanUtils.populate(section, map);
            // 执行 insert 或 update
            String result = null;
            if (section.getId() == 0) {
                // 插入操作
                result = courseContentService.insertSection(section);
            } else {
                // 修改操作
                result = courseContentService.updateSection(section);
            }
            // 将结果返回
            PrintWriter writer = resp.getWriter();
            writer.write(result);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 修改章节状态
    public void updateSectionStatus(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            String status = req.getParameter("status");
            // 执行业务层修改操作，并获取结果
            String result = courseContentService.updateStatusById(Integer.parseInt(id), Integer.parseInt(status));
            // 将结果写出去
            PrintWriter writer = resp.getWriter();
            writer.write(result);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
